define(function (require) {
    require('validate');
    var Base = require('group/common/modal');
    var utils = require('common/utils/home');
    var Mask = require('common/mask');
    var mask = new Mask();
    var ModalTpl = require('text!./templates/agreement_modal.html');

    return Base.extend({
        name: 'register_agreement',
        initialize: function (params) {
            if (!params) {
                params = {};
            }
            Base.prototype.initialize.apply(this, [_.extend({
                mask_disable: true
            }, params)]);
        },
        show: function (url) {
            var _this = this;
            if (!_this.is_create()) {
                mask.show();
                utils.ajaxGet(url, function (resp) {
                    mask.hide();
                    _this.render_modal({
                        html: _this.render(ModalTpl, {
                            info: resp.data
                        }),
                        name: _this.name
                    });
                    $('#' + _this.id).css({
                        left: '0px',
                        top: '0px'
                    });
                    $('#' + _this.id + ' .modal-content').css({
                        height: $(window).height() + 'px',
                        width: $(window).width() + 'px'
                    });
                    $('#' + _this.id + ' .agreement_text').css({
                        height: $(window).height() - 40 + 'px'
                    });
                    Base.prototype.show.apply(_this, []);
                    _this.bind_events();
                });
            }
            else {
                Base.prototype.show.apply(this, []);
            }
        },
        bind_events: function () {
            var _this = this;
            _this.$('.js-choice-attr-close').click(function () {
                _this.hide();
            });

            var countDownFinish = false;
            var countDownStep = 0;
            var countDownInterval = setInterval(function () {
                var total_step = 10;
                countDownStep++;
                countDownFinish = countDownStep >= total_step;
                if (countDownFinish) {
                    clearInterval(countDownInterval);
                    _this.$('.agreement_countdown').html('');
                }
                else {
                    _this.$('.agreement_countdown').html(total_step - countDownStep + '秒');
                }
                update_scroll_percent();
            }, 1000);

            var update_scroll_percent = function () {
                var text_sel = '.agreement_text';
                var percent = _.intval(100 * $(text_sel).scrollTop() / ($(text_sel)[0].scrollHeight - $(text_sel).height() - 19 ));
                _this.$('.scroll_percent').html("(%" + percent + ")");
                _this.$('.agreement_btn').unbind('click');
                if (percent >= 95 && countDownFinish) {
                    _this.$('.agreement_btn').addClass('ac').click(function () {
                        _this.hide();
                        console.log(_this.params);

                        if (_this.params.after_hide) {
                            _this.params.after_hide();
                        }
                    });
                }
                else {
                    _this.$('.agreement_btn').removeClass('ac');
                }
            };
            update_scroll_percent();
            _this.$('.agreement_text').bind('scroll', function () {
                update_scroll_percent();
            });
        }
    });
});